package com.wandousoushu.jiusen.ui.my

import android.os.Bundle
import android.text.TextUtils
import android.webkit.JavascriptInterface
import android.webkit.WebView
import android.webkit.WebViewClient
import androidx.appcompat.app.AppCompatActivity
import com.wandousoushu.jiusen.MainActivity
import com.wandousoushu.jiusen.MyApplication
import com.wandousoushu.jiusen.R
import com.wandousoushu.jiusen.bean.UserInfo
import com.wandousoushu.jiusen.util.GsonUtils
import com.wandousoushu.jiusen.web.http.MainHttpConsts
import com.wandousoushu.jiusen.web.http.MainHttpUtils
import org.jetbrains.anko.startActivity
import org.json.JSONObject


class PayActivity : AppCompatActivity() {
     lateinit var webView: WebView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_pay)
        webView = findViewById(R.id.pay_webview)
        webView.getSettings().javaScriptEnabled = true
        webView.setWebViewClient(WebViewClient())
        val ruleId = intent.getIntExtra("ruleId",0)
        var url=MainHttpConsts.VipPay+"?user="+MyApplication.instance.userId+"&cash_rule_id="+ruleId
        webView.loadUrl(url)
    }

    private fun HttpUserInfo() {
        Thread(Runnable {
            try {
                val returnData = MainHttpUtils.getInstance().GetUserInfo(MyApplication.instance.token)
                if (returnData == null) {
                    return@Runnable
                }
                val jsonObject = JSONObject(returnData)
                if (jsonObject != null && jsonObject.getInt("code") == 1) {
                    val data = jsonObject.getString("data")
                    if (TextUtils.isEmpty(data)) {
                        return@Runnable
                    }
                    val userInfo = GsonUtils.parseJObject(jsonObject.getString("data"), UserInfo::class.java)
                    if (userInfo != null) {
                        MyApplication.instance.isVip = if (userInfo.is_vip == 1) true else false
                        MyApplication.instance.userinfo.is_vip=userInfo.is_vip
                        MyApplication.instance.userinfo.is_vip_endtime=userInfo.is_vip_endtime
                        startActivity<MainActivity>(Pair("position", 2))
                        //supportFragmentManager.findFragmentById(R.id.)
                    }
                }
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }).start()
    }

    override fun onDestroy() {
        super.onDestroy()
        webView.stopLoading();
        webView.removeAllViews();
        webView.destroy();
        HttpUserInfo()
    }

}